package com.rr.learningdemo.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

/**
 * @author zhuangcanrun
 * @Date 2021/06/21/16:14
 * @description
 **/

@Component
public class RedisChooseDbListen {

    @Autowired
    RedisTemplate redisTemplate;

    @Bean
    @ConditionalOnMissingBean(RedisMessageListenerContainer.class)
    public RedisMessageListenerContainer container() {
        RedisMessageListenerContainer listenerContainer = new RedisMessageListenerContainer();

        listenerContainer.setConnectionFactory(redisTemplate.getConnectionFactory());
        //监听指定db0,db1的set事件
        listenerContainer.addMessageListener(new RedisListener(), new PatternTopic("__keyevent@1__:set"));
        listenerContainer.addMessageListener(new RedisListener(), new PatternTopic("__keyevent@0__:set"));
        return listenerContainer;
    }
}
